package in.muton.edushare.newentities.upgraded.singleInvite;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.newentities.upgraded.InviteUtils;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class UnivInvite implements SingleAudienceInvite {
	private static final long serialVersionUID = -3232598417092357641L;

	private IModel<Universities> univModel;

	public UnivInvite(IModel<Universities> univModel) {
		this.univModel = univModel;
	}

	public List<String> inviteUsers(int role, CayenneList<Users> userList,ORMInterface ormInterface) {
		List<IModel<Users>> userModel = userList.getObject();
		UserRolesDAO addDAO = new UserRolesDAO(ormInterface);
		List<String> strList = new ArrayList<String>();
		for (IModel<Users> u : userModel) {
			strList.add(addDAO.addUserFB(univModel.getObject(), u.getObject(),
					role, EduShareSession.get().getLoggedInUser()));
		}
		return strList;
	}

	public List<String> inviteUsers(int role, List<CayenneList<Users>> userList,ORMInterface ormInterface) {
		List<String> strList = new ArrayList<String>();
		for (CayenneList<Users> u : userList) {
			strList.addAll(inviteUsers(role, u,ormInterface));
		}
		return strList;
	}

	public List<String> inviteUsers(int role, String[] userList, ORMInterface ormInterface) {
		List<String> statusList = new ArrayList<String>();
		UserRolesDAO addDAO = new UserRolesDAO(ormInterface);
		for (String s : userList) {
			Users userVO = InviteUtils.getUserByString(s, ormInterface);
			if (userVO != null) {
				statusList.add(addDAO.addUserFB(univModel.getObject(), userVO,
						role, EduShareSession.get().getLoggedInUser()));
			} else {
				if (InviteUtils.isEmailId(s)) {
					UsersDAO uDAO = new UsersDAO(ormInterface);
					userVO = uDAO.newUser(s.trim());
					statusList.add(addDAO.addUserFB(univModel.getObject(),
							userVO, role, EduShareSession.get()
									.getLoggedInUser()));
				} else {
					statusList.add("Unable to determine the type of user " + s);
				}
			}
		}
		return statusList;
	}
}
